package com.fjnu.controller;

import com.fjnu.dto.*;
import com.fjnu.entity.Article;
import com.fjnu.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/blog-api/v0.1/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 根据文章的查询条件进行查询
     */
    @PostMapping("/list")
    public ResponseResult<List<Article>> list(@RequestBody ArticleConditionDto articleCond) {
        try {
            // 这里简化处理，实际应该添加分页参数
            List<Article> articles = articleService.queryByArticleCondition(articleCond, 1, 100).getList();
            return ResponseResult.success(articles);
        } catch (Exception e) {
            return ResponseResult.error("查询文章列表失败: " + e.getMessage());
        }
    }

    /**
     * 新增文章
     */
    @PostMapping("/add")
    public ResponseResult add(@RequestBody ArticleDto articleDto) {
        try {
            Article article = new Article();
            article.setUserId(articleDto.getUserId());
            article.setTitle(articleDto.getTitle());
            article.setContent(articleDto.getContent());
            article.setCategoryId(articleDto.getCategoryId());
            
            Article result = articleService.insertArticle(article);
            if (result != null) {
                return ResponseResult.success(result);
            } else {
                return ResponseResult.error("新增文章失败");
            }
        } catch (Exception e) {
            return ResponseResult.error("新增文章异常: " + e.getMessage());
        }
    }

    /**
     * 修改文章
     */
    @PostMapping("/update")
    public ResponseResult update(@RequestBody ArticleUpdateDto articleUpdateDto) {
        try {
            int result = articleService.updateArticle(articleUpdateDto);
            if (result > 0) {
                return ResponseResult.success(null);
            } else {
                return ResponseResult.error("修改文章失败");
            }
        } catch (Exception e) {
            return ResponseResult.error("修改文章异常: " + e.getMessage());
        }
    }

    /**
     * 发布文章
     */
    @GetMapping("/{id}/publish")
    public ResponseResult publish(@PathVariable("id") String articleId, 
                                 @RequestParam(required = false) String userId) {
        try {
            // 注意：发布文章需要userId，但在GET请求中作为查询参数传递可能不太合适
            // 更合理的做法是在实际项目中从session或token中获取
            if (userId == null) {
                return ResponseResult.error("缺少用户ID参数");
            }
            
            int result = articleService.publishArticle(articleId, userId);
            if (result > 0) {
                return ResponseResult.success(null);
            } else {
                return ResponseResult.error("发布文章失败");
            }
        } catch (Exception e) {
            return ResponseResult.error("发布文章异常: " + e.getMessage());
        }
    }
}